/*
 * @description  : 内管登录接口
 * @descriptionDetail: 内管登录接口
 * @copyright    : 浙江烟草
 * @author       : lcm
 * @create       : 2022-09-27 09:58:57
 */


import apiUrl from "@/common/api_urls/internal";
import reqHelper from "@/common/reqHelper";
import { PATHROOT, } from '@/common/path.config.js';
export default {
  loadAllDicts() {
    return reqHelper.postJson({}, apiUrl.common.bscCodesAll);
  },
  getCurrUserInfo() {
    return new Promise((resolve, reject) => {
      if (store.state.userInfo == undefined) {
        let user = JSON.parse(uni.getStorageSync('user'));
        store.state.userInfo = user;
        resolve(user);
      } else {
        resolve(store.state.userInfo);
      }
    });
  },
  saveUserInfo(user) {
    return new Promise((resolve, reject) => {
      store.state.userInfo = user;
      uni.setStorageSync('user', JSON.stringify(user));
      resolve();
    });
  },
  /**
   * 查询专卖机构信息
   * @param {*} parentUuid    父节点
   * @param {*} treeType      类型：zm-pq专卖片区    zm-case
   * @returns
   */
  orgTree(parentUuid, treeType) {
    return reqHelper.postForm({ parentUuid: parentUuid, treeType: treeType, }, apiUrl.common.orgTree);
  },
  /**
   * 组织机构和用户
   * @param {*} parentUuid    父节点
   * @param {*} treeType      类型：zm-case
   * @returns
   */
  userTree(parentUuid, treeType) {
    return reqHelper.postForm({ treeType: treeType, parentUuid: parentUuid || "", }, apiUrl.common.userTree);
  },
  /**
   * 当前用户是否参与快办试运行
   * @param {*} jobName
   * @param {*} deptUuid
   * @param {*} subKey
   * @returns
   */
  scheduleRun(jobName, deptUuid, subKey) {
    return reqHelper.postForm({ jobName: jobName, deptUuid: deptUuid, subKey: subKey, }, apiUrl.common.scheduleRun);
  },

  /**
   * 获取行政区划信息(省市区)
   * @returns
   */
  getAreaTreeAll() {
    return new Promise((resolve, reject) => {
      if (uni.getStorageSync("areas")) {
        resolve({ success: true, data: JSON.parse(uni.getStorageSync("areas")), });
        return;
      }
      reqHelper.postForm({}, apiUrl.common.areaList).then(dr => {
        if (dr.success) {
          let areas = dr.data;
          uni.setStorageSync("areas", JSON.stringify(areas));
          resolve({ success: true, data: areas, });
        } else {
          reject(dr.msg);
        }
      });
    });
  },
  /**
   * 获取行政区域(省市区)list
   * @returns
   */
  area() {
    if (uni.getStorageSync('areas')) {
      return;
    }
    reqHelper.postJson({}, apiUrl.common.areaList).then(dr => {
      if (dr.success) {
        let areas = dr.data;
        // 递归处理行政区划数据
        let areaDatas = deeploop("", areas);
        uni.setStorageSync('areas', JSON.stringify(areaDatas));
      }
    });
  },

  /**
   * 记录钉钉移动端日志
   * @returns {Promise<any>}
   */
  dingLog(bizUuid, moduleType, optType, optDesc, remark) {
    return reqHelper.postJson({
      bizUuid: bizUuid,
      moduleType: moduleType,
      optType: optType,
      optDesc: optDesc,
      remark: remark,
    }, apiUrl.common.dingLog);
  },

  /**
   * 获取街道列表
   * @param {Object} areaCode
   */
  getStreetsList(areaCode) {
    return reqHelper.postJson({ areaCode: areaCode, }, apiUrl.common.streets);
  },

  /**
   * 获取社区列表
   * @param {Object} areaCode
   */
  getVillagesList(areaCode) {
    return reqHelper.postJson({ areaCode: areaCode, }, apiUrl.common.villages);
  },
  /**
   * 获取当月第一天和最后一天
   */
  currentMonthDays() {
    return reqHelper.postJson({}, apiUrl.common.currentMonthDays);
  },

  /**
   * 获取当前周的第一天和最后一天
   */
  currentWeekDays() {
    return reqHelper.postJson({}, apiUrl.common.currentWeekDays);
  },
  /**
   * 获取当前周的第一天和最后一天
   * @param orguuid
   */
  getOrgs(code) {
    return reqHelper.postJson({ orgUuids: code, }, apiUrl.common.orgs);
  },

  /**
   * 模糊查询卷烟品规
   * @param {*} keyword     关键字：卷烟名称、品牌名称、包条形码、条条码
   * @param {*} page
   * @param {*} limit
   * @returns
   */
  cigSearch(page, limit, keyword) {
    return reqHelper.postForm({ page: page, limit: limit, productName: keyword, }, apiUrl.common.cigSearch);
  },

  /**
   * 获取某个编码
   * @param {Object} bctPhNames
   */
  bscCodesMap(bctPhNames) {
    let cacheBaseCode = getApp().globalData.cacheBaseCode;
    let now = new Date().getTime();
    let code = bctPhNames;
    let codes = {};
    let needReq = false;
    let names = code.split(',');
    for (let name1 of names) {
      let val = cacheBaseCode[name1];
      if (val === undefined) {
        needReq = true;
        break;
      } else {
        codes[name1] = val;
      }
    }
    // 30分钟请求一次
    if (now - cacheBaseCode.lst > 1000 * 60 * 30) {
      needReq = true;
      cacheBaseCode.lst = now;
    }
    if (!needReq) {
      // 这里做下缓存
      getApp().globalData.cacheBaseCode = cacheBaseCode;
      return new Promise < BscCodeMap > ((resolve, reject) => {
        resolve(codes);
      });
    } else {
      return reqHelper.postForm({ bctPhNames: bctPhNames, }, apiUrl.common.bscCodesMap).then(res => {
        Object.keys(res).forEach(a => {
          cacheBaseCode[a] = res[a];
        });
        getApp().globalData.cacheBaseCode = cacheBaseCode;
        return res;
      });
    }
  },
  /**
   * 附件预览地址
   * @param {Object} imgUrl
   */
  getViewImgUrl(imgUrl) {
    return apiUrl.common.viewImgUrl + "?tk=" + uni.getStorageSync("tk") + "&imgUrl=" + encodeURIComponent(imgUrl);
  },
  /**
   * 附件下载地址
   * @param {Object} fileId
   */
  getDownloadFileUrl(fileId) {
    return apiUrl.common.downloadFile + "?tk=" + uni.getStorageSync("tk") + "&fileId=" + fileId;
  },
  /**
   * 查询业务附件
   * @param {Object} bizId
   * @param {Object} group
   */
  goFiles(bizId, group) {
    return reqHelper.get({ year: 2018, bizId: bizId, group: group, }, apiUrl.common.findFilesByBizIdAndGroup);
  },

  /**
   * 上传base64编码文件
   * @param {Object} base64Data
   * @param {Object} param  {bizId,fileName,fiGroupCode,fiLogicPath}
   */
  uploadBase64ForApp(base64Data, param){
    let sp = `?bizId=${param.bizId}&fileName=${param.fileName}&fiGroupCode=${param.fiGroupCode}&fiLogicPath=${param.fiLogicPath}`;
    base64Data = base64Data.indexOf(",") ? base64Data.split(",")[1] : base64Data;
    return reqHelper.postJson({
      base64Data: base64Data,
    }, apiUrl.common.uploadBase64ForApp + sp);
  },

  /**
   * 删除附件
   * @param {Object} fileId
   */
  delFile(fileId) {
    return reqHelper.get({ fileId: fileId, }, apiUrl.common.deleteFile);
  },

  /**
   * 上传base64编码文件
   * @param {Object} param  {base64Data,fileName,fiGroupCode}
   */
  uploadBase64File(param){
    let sp = `?fiGroupCode=${param.fiGroupCode}&fileName=${param.fileName}`;
    return reqHelper.postJson(param.base64Data, apiUrl.common.uploadBase64 + sp);
  },
  /**
   * 根据bizId查询文件
   * @returns {*}
   */
  findFilesByBizId(bizId) {
    return reqHelper.postForm({ year: "", bizId: bizId, }, apiUrl.common.findFilesByBizId);
  },
  /**
   * 根据fileId查询文件
   * @returns {*}
   */
  findFileByFileId(fileId) {
    return reqHelper.postForm({ fileId: fileId, }, apiUrl.common.findFileByFileId);
  },

  /**
   * 切换部门
   * @param {Object} param
   */
  switchDept(param) {
    let headers = {};
    headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
    headers["Accept"] = "application/json,text/javascript,*/*;q=0.01";
    headers["tk"] = param.tk;
    return reqHelper.postForm(param, apiUrl.common.switchDept, headers);
  },
  /**
   * 文件上传地址
   */
  uploadFile: apiUrl.common.uploadFile,
  /**
   * 删除文件
   */
  deleteFile: apiUrl.common.deleteFile,
  downloadFile: apiUrl.common.downloadFile,
  /**
   * 获取移动端模块权限
   */
  getMobileModuleAuth(){
    return reqHelper.get({}, apiUrl.common.getMobileModuleAuth);
  },
  baseUrl: PATHROOT.BASE,
  /**
   * 移动端文书节点查询
   * @param {*} keyword
   * @param {*} page
   * @param {*} limit
   * @returns
   */
  searActivity( paras) {
    return reqHelper.postJson( { paras: paras, }, apiUrl.internal.searActivity);
  },
};
